#include "IMITableView.h"
IMITableView* IMITableView::create(CCTableViewDataSource* dataSource, CCSize size)
{
    return IMITableView::create(dataSource, size, NULL);
}

IMITableView* IMITableView::create(CCTableViewDataSource* dataSource, CCSize size, CCNode *container)
{
    IMITableView *table = new IMITableView();
    table->initWithViewSize(size, container);
    table->autorelease();
    table->setDataSource(dataSource);
    table->_updateContentSize();
	table->setTouchEnabled(true);
	table->setTouchMode(kCCTouchesOneByOne);
    return table;
}

void IMITableView::registerWithTouchDispatcher()
{
	CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, -128, false);
}

void IMITableView::show()
{
	this->setVisible(true);
}

void IMITableView::hide()
{
	this->setVisible(false);
}

void IMITableView::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{
	//CCLog("------------------------------------------3-");
    if (!this->isVisible()) {
		//CCLog("1111111111111111111111111111111111111111---");
        return;
    }
	//CCLog("2222222222222222222222222222222222222222---%i",this->isTouchMoved());
    if (m_pTouches->count() == 1 && !this->isTouchMoved()) {
        unsigned int        index;
        CCTableViewCell   *cell;
        CCPoint           point;
        //CCLog("33333333333333333333333333333333333333333333---");
        point = this->getContainer()->convertTouchToNodeSpace(pTouch);
        if (m_eVordering == kCCTableViewFillTopDown) {
            CCSize cellSize = m_pDataSource->cellSizeForTable(this);
            point.y -= cellSize.height;
			//CCLog("44444444444444444444444444444444444444444444---");
        }
		//CCLog("4444444444444444444444555555555555555555555---");
        index = this->_indexFromOffset(point);
        cell  = this->_cellWithIndex(index);
        
        if (cell) {
            m_pTableViewDelegate->tableCellTouched(this, cell);
			//CCLog("55555555555555555555555555555555555555555555---");
        }
		//CCLog("6666666666666666666666666666666666666666666666---");
    }
	//CCLog("77777777777777777777777777777777777777777---");
    CCScrollView::ccTouchEnded(pTouch, pEvent);
}

bool IMITableView::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
	//CCLog("-----------------------------------------1--");
    if (!this->isVisible())
    {
		//CCLog("ccTouchBegan1111111---");
        return false;
    }
    CCRect frame;
    CCPoint frameOriginal = this->getParent()->convertToWorldSpace(this->getPosition());
    frame = CCRectMake(frameOriginal.x, frameOriginal.y, m_tViewSize.width, m_tViewSize.height);
    //CCLog("ccTouchBegan222222---");
    //dispatcher does not know about clipping. reject touches outside visible bounds.
    if (m_pTouches->count() > 2 ||
        m_bTouchMoved          ||
        !frame.containsPoint(m_pContainer->convertToWorldSpace(m_pContainer->convertTouchToNodeSpace(touch))))
    {
		//CCLog("ccTouchBegan3333333---");
        return false;
    }

    if (!m_pTouches->containsObject(touch))
    {
        m_pTouches->addObject(touch);
		//CCLog("ccTouchBegan4444444---");
    }

    if (m_pTouches->count() == 1)
    { // scrolling
        m_tTouchPoint		= this->convertTouchToNodeSpace(touch);
        m_bTouchMoved		= false;
        m_bDragging			= true; //dragging started
        m_tScrollDistance	= ccp(0.0f, 0.0f);
        m_fTouchLength		= 0.0f;
		//CCLog("ccTouchBegan55555555---");
    }
    else if (m_pTouches->count() == 2)
    {
        m_tTouchPoint  = ccpMidpoint(this->convertTouchToNodeSpace((CCTouch*)m_pTouches->objectAtIndex(0)),
                                   this->convertTouchToNodeSpace((CCTouch*)m_pTouches->objectAtIndex(1)));
        m_fTouchLength = ccpDistance(m_pContainer->convertTouchToNodeSpace((CCTouch*)m_pTouches->objectAtIndex(0)),
                                   m_pContainer->convertTouchToNodeSpace((CCTouch*)m_pTouches->objectAtIndex(1)));
        m_bDragging  = false;
		//CCLog("ccTouchBegan66666666---");
    } 
	//CCLog("ccTouchBegan777777777---");
    return true;
}

void IMITableView::ccTouchMoved(CCTouch* touch, CCEvent* event)
{
	//CCLog("----------------------------------------2---");
    if (!this->isVisible())
    {
		//CCLog("ccTouchMoved1111111---");
        return;
    }
    if (m_pTouches->containsObject(touch))
    {
		//CCLog("ccTouchMoved222222---");
        if (m_pTouches->count() == 1 && m_bDragging)
        { // scrolling
            CCPoint moveDistance, newPoint, maxInset, minInset;
            CCRect  frame;
            float newX, newY;
            //CCLog("ccTouchMoved33333---");
            
            CCPoint frameOriginal = this->getParent()->convertToWorldSpace(this->getPosition());
            frame = CCRectMake(frameOriginal.x, frameOriginal.y, m_tViewSize.width, m_tViewSize.height);

			//imiadd
			CCPoint tempPoint= ccpSub(this->convertTouchToNodeSpace((CCTouch*)m_pTouches->objectAtIndex(0)), m_tTouchPoint);
			if(tempPoint.x<0) tempPoint.x=-tempPoint.x;
			if(tempPoint.y<0) tempPoint.y=-tempPoint.y;
			if(tempPoint.x<10&&tempPoint.y<10) return;

			m_bTouchMoved  = true;
            newPoint     = this->convertTouchToNodeSpace((CCTouch*)m_pTouches->objectAtIndex(0));
            moveDistance = ccpSub(newPoint, m_tTouchPoint);
            m_tTouchPoint  = newPoint;
            
            if (frame.containsPoint(this->convertToWorldSpace(newPoint)))
            {
				//CCLog("ccTouchMoved444444---");
                switch (m_eDirection)
                {
                    case kCCScrollViewDirectionVertical:
                        moveDistance = ccp(0.0f, moveDistance.y);
                        break;
                    case kCCScrollViewDirectionHorizontal:
                        moveDistance = ccp(moveDistance.x, 0.0f);
                        break;
                    default:
                        break;
                }

                m_pContainer->setPosition(ccpAdd(m_pContainer->getPosition(), moveDistance));
                
                maxInset = m_fMaxInset;
                minInset = m_fMinInset;
                
                
                //check to see if offset lies within the inset bounds
                newX     = MIN(m_pContainer->getPosition().x, maxInset.x);
                newX     = MAX(newX, minInset.x);
                newY     = MIN(m_pContainer->getPosition().y, maxInset.y);
                newY     = MAX(newY, minInset.y);
                
                m_tScrollDistance     = ccpSub(moveDistance, ccp(newX - m_pContainer->getPosition().x, newY - m_pContainer->getPosition().y));
                this->setContentOffset(ccp(newX, newY));
            }
        }
        else if (m_pTouches->count() == 2 && !m_bDragging)
        {
			//CCLog("ccTouchMoved555555---");
            const float len = ccpDistance(m_pContainer->convertTouchToNodeSpace((CCTouch*)m_pTouches->objectAtIndex(0)),
                                            m_pContainer->convertTouchToNodeSpace((CCTouch*)m_pTouches->objectAtIndex(1)));
            this->setZoomScale(this->getZoomScale()*len/m_fTouchLength);
        }
    }
}